package com.potenza.training.reply.config;


import com.potenza.training.reply.entity.Subject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReadExcelUtil {

    public List<Subject> testReadType(FileInputStream inputStream) throws Exception{
        //获取文件流
        //FileInputStream inputStream = new FileInputStream(PATH+"题目表.xlsx");
        //1:创建一个工作簿
        List<Subject> list = new ArrayList<>();
        Workbook workbook = new XSSFWorkbook(inputStream);
        //2:得到表
        Sheet sheet = workbook.getSheetAt(0);
        //3:得到行
        Row rowTitle = sheet.getRow(0);
        //4:得到列
        if(rowTitle!=null){
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                Cell cell = rowTitle.getCell(cellNum);
                if(cell != null){
                    int cellType = cell.getCellType();
                    //打印 getStringCellValue字符串类型
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue + "|");
                }
            }
            System.out.println();
        }

        //创建实体类集合接受数据
        //List<Subject> list = new ArrayList<>();
        //获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            Subject subject = new Subject();
            if(rowData!=null){
                //读取列
                int cellCount = rowTitle.getPhysicalNumberOfCells();
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    Cell cell = rowData.getCell(cellNum);
                    //匹配到的数据类型
                    if(cell != null){
                        int cellType = cell.getCellType();
                        String  cellValue = "";

                        switch (cellType){
                            case HSSFCell.CELL_TYPE_STRING://字符串类型
                                //STRING
                                cellValue = cell.getStringCellValue();
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN://布尔类型
                                //BOOLEAN
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK://此处为空
                                //BLANK]
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC://数据类型
                                //NUMERIC]
                                //判断是否是时间格式
                                if(HSSFDateUtil.isCellDateFormatted(cell)){
                                    //日期]
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                }else{
                                    //不是日期格式,防止数字过长
                                    //转换为字符串输出]
                                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                    cellValue = cell.toString();
                                }
                                break;
                                //数据类型错误
                            case HSSFCell.CELL_TYPE_ERROR:
                                System.out.print("[数据类型错误]");
                                break;
                        }
                        saveSubject(rowNum,cellNum,cellValue,subject);
                    }
                }
                list.add(subject);
            }
        }
        inputStream.close();
        return list;
    }

    private void saveSubject(int rowNum, int cellNum, String cellValue, Subject subject) {
        switch (cellNum){
            case 2:
                subject.setSubjectType(cellValue);
            break;
            case 3:
                subject.setTitleName(cellValue);
            break;
            case 4:
                subject.setStandardAnswer(cellValue);
            break;
            case 5:
                subject.setSubjectDifficlty(cellValue);
            break;
            case 6:
                subject.setTimeLimit(cellValue);
            break;
            case 7:
                subject.setSubiectDesc(cellValue);
            break;
        }
    }


}
